//
//  MyCollectDetailVC.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/10/28.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "MyCollectDetailVC.h"
#import "MyShipmentNumberCell.h"
#import "MyShipmentAddressCell.h"
#import "MyShipmentInfoCell.h"
#import "MyShipmentGoodCell.h"
#import "WaybillMainModel.h"
#import "WoShouQianShouVC.h"
#import "WayBillGuiJiVC.h"
#import "WaybillManagerGoodListVC.h"

@interface MyCollectDetailVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) WaybillMainModel *model;
@property (nonatomic, strong) UIView *bottomView;

@end

@implementation MyCollectDetailVC

-(UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH - BottomHeight - NaviHeight - 64, ScreenW, 64+BottomHeight)];
        _bottomView.backgroundColor = HexColor(0xffffff);
    }
    return _bottomView;
}

//查看运输轨迹
- (void)lookCarClick {
    WayBillGuiJiVC *vc = [WayBillGuiJiVC new];
    vc.wa_id = self.wa_id;
    vc.type = @"2";
    [self.rt_navigationController pushViewController:vc animated:YES];
}

//签收
- (void)huiDanShangJiao {
    WoShouQianShouVC *vc = [WoShouQianShouVC new];
    vc.wa_id = self.wa_id;
    [self.rt_navigationController pushViewController:vc animated:YES];
}

-(WaybillMainModel *)model {
    if (!_model) {
        _model = [WaybillMainModel new];
    }
    return _model;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - self.bottomView.nim_height) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        __weak_self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakself requestForData];
        }];
        _tableView.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"img_occupy"
             titleStr:@""
            detailStr:@""];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"详情";
    [self.view addSubview:self.bottomView];
    [self.view addSubview:self.tableView];
    [self requestForData];
}

- (void)requestForData {
    [HttpRequest PostWithApi:ReceiptDeliveryMyShipmentDetail params:@{@"wa_id":self.wa_id,@"type":@"2"} progress:^(NSProgress * _Nonnull progress) {
            
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            self.model = [WaybillMainModel mj_objectWithKeyValues:responseObject];
            [self configUI];
            [self.tableView reloadData];
        }
        [self.tableView.mj_header endRefreshing];
    } fail:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
    }];
}

- (void)configUI {
    if ([self.model.status intValue] > 4 && [self.model.status intValue] < 12) {
        //途中
        [self.bottomView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        UIButton *rejectBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, (_bottomView.nim_width - 15 - 60) * 0.5, 44)];
        [rejectBtn setBackgroundColor:HexColorA(0x3D68EB, 0.1)];
        rejectBtn.layer.masksToBounds = YES;
        rejectBtn.layer.cornerRadius = 6;
        [rejectBtn setImage:[UIImage imageNamed:@"wofayun_icon_location"] forState:0];
        [rejectBtn setTitle:@"查看运输轨迹" forState:0];
        [rejectBtn setTitleColor:Color_ThemeColor forState:0];
        rejectBtn.titleLabel.font = BoldFontOfSize(16);
        [rejectBtn addTarget:self action:@selector(lookCarClick) forControlEvents:UIControlEventTouchUpInside];
        rejectBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
        [self.bottomView addSubview:rejectBtn];
        
        UIButton *passBtn = [[UIButton alloc] initWithFrame:CGRectMake(rejectBtn.nim_right + 15, 10, (_bottomView.nim_width - 15 - 60) * 0.5, 44)];
        [passBtn setBackgroundColor:Color_ThemeColor];
        passBtn.layer.masksToBounds = YES;
        passBtn.layer.cornerRadius = 6;
        [passBtn setTitle:@"签收" forState:0];
        [passBtn setTitleColor:HexColor(0xffffff) forState:0];
        passBtn.titleLabel.font = BoldFontOfSize(16);
        [passBtn addTarget:self action:@selector(huiDanShangJiao) forControlEvents:UIControlEventTouchUpInside];
        passBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
        [self.bottomView addSubview:passBtn];
    }else {
        //其他情况，显示查看路线按钮
        [self.bottomView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        UIButton *nextBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, _bottomView.nim_width - 60, 44)];
        [nextBtn setBackgroundColor:Color_ThemeColor];
        nextBtn.layer.masksToBounds = YES;
        nextBtn.layer.cornerRadius = 6;
        [nextBtn setImage:[UIImage imageNamed:@"icon_location"] forState:0];
        [nextBtn setTitle:@"查看运输轨迹" forState:0];
        [nextBtn setTitleColor:HexColor(0xffffff) forState:0];
        nextBtn.titleLabel.font = BoldFontOfSize(16);
        [nextBtn addTarget:self action:@selector(lookCarClick) forControlEvents:UIControlEventTouchUpInside];
        [self.bottomView addSubview:nextBtn];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        MyShipmentNumberCell *cell = [MyShipmentNumberCell MyShipmentNumberCellWithTableView:tableView];
        cell.model = self.model;
        return cell;
    }else if (indexPath.row == 1) {
        MyShipmentAddressCell *cell = [MyShipmentAddressCell MyShipmentAddressCellWithTableView:tableView];
        cell.model = self.model;
//        __weak_self;
//        cell.listBlock = ^(NSString * _Nonnull num) {
//            WoFaYunDetailGoodListVC *vc = [WoFaYunDetailGoodListVC new];
//            vc.wa_id = weakself.wa_id;
//            vc.serial_num = num;
//            vc.type = @"2";
//            [weakself.rt_navigationController pushViewController:vc animated:YES];
//        };
        return cell;
    }else if (indexPath.row == 2) {
        MyShipmentInfoCell *cell = [MyShipmentInfoCell MyShipmentInfoCellWithTableView:tableView];
        cell.model = self.model;
        return cell;
    }else {
        MyShipmentGoodCell *cell = [MyShipmentGoodCell MyShipmentGoodCellWithTableView:tableView];
        cell.model = self.model;
        __weak_self;
        cell.listBlock = ^{
            WaybillManagerGoodListVC *vc = [WaybillManagerGoodListVC new];
            vc.wa_id = weakself.model.wa_id;
            vc.type = @"2";
            [weakself.rt_navigationController pushViewController:vc animated:YES];
        };
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        MyShipmentNumberCell *cell = [MyShipmentNumberCell MyShipmentNumberCellWithTableView:tableView];
        cell.model = self.model;
        return cell.height;
    }else if (indexPath.row == 1) {
        MyShipmentAddressCell *cell = [MyShipmentAddressCell MyShipmentAddressCellWithTableView:tableView];
        cell.model = self.model;
        return cell.height;
    }else if (indexPath.row == 2) {
        MyShipmentInfoCell *cell = [MyShipmentInfoCell MyShipmentInfoCellWithTableView:tableView];
        cell.model = self.model;
        return cell.height;
    }else {
        MyShipmentGoodCell *cell = [MyShipmentGoodCell MyShipmentGoodCellWithTableView:tableView];
        cell.model = self.model;
        return cell.height;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 10)];
    view.backgroundColor = Color_backgroundColor;
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

@end
